import axios from "axios";
// 时间戳转X分钟前：getDateDiff
// 时间戳转为标准时间格式：formatDate
// 获取页面参数 getParam（url，获取参数名）




/* 时间戳转换为：16分钟前   3小时前 等等 注意，时间戳要*1000 */
export function getDateDiff(dateTimeStamp){
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30; 
    var now = new Date().getTime();
    var diffValue = now - dateTimeStamp;
    var monthC =diffValue/month;
    var weekC =diffValue/(7*day);
    var dayC =diffValue/day;
    var hourC =diffValue/hour;
    var minC =diffValue/minute;
    let result
    if(hourC>=24){
        result = parseInt(dayC)+"天前";
    }
    else if(hourC>=1){
        result = parseInt(hourC) +"个小时前";
    }
    else if(minC>=1){
        result = parseInt(minC) +"分钟前";
    }else
        result = "刚刚发表";
    return result;
}

/* 时间戳转为2022-2-8 14：24：24格式 */
export function formatDate(time){
    let date = new Date(parseInt(time));
    let YY = date.getFullYear() + '-';
    let MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    let hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    let mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    let ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return YY + MM + DD +" "+hh + mm + ss;
}

/* 获取页面参数 */
export function getParam(url,param){
    let arr=url.replace("?","").split('&')
    let arrPlus =[]
    arr.map((item)=>{
        arrPlus=[...item.split('='),...arrPlus]
    })
    let value = false
    arrPlus.map((item,index)=>{
        if(item===param){
            value = arrPlus[index+1]
        }
    })
    return value
}

/* 导出excel */
export function exportExcel(url,params,fileName) {
    const login = localStorage.getItem('uooki-token')
        axios({
          method:'get',
          url:'https://www.donglongdong.xyz/api'+url,
          // AUTHORIZATION: login,
          headers:{
            AUTHORIZATION: login,
          },

          params,
          responseType:'blob',
          // AUTHORIZATION
        }).then(res=>{
          let blob = new Blob([res.data], {type: "application/vnd.ms-excel"});
        let url = window.URL.createObjectURL(blob);
        // window.location.href = url;  //本窗口打开直接下载
 
        // 重命名   创建a标签
        var a = document.createElement('a');
        // 下载a 标签中 href 指向的文件，根据 download 中设定的值，对文件进行重命名。
        a.href = url
        // 设置文件名，不写默认原文件名称
        a.download = fileName
        a.click()
        })
}